﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;

namespace ConsoleApp9
{
    public class DdpRequestInfo : IFixedHeaderRequestInfo
    {
        /// <summary>
        /// 接口实现，标识数据长度
        /// </summary>
        public int BodyLength { get; private set; }
        public string DTU { get; private set; }
        
       

        public bool OnParsingBody(byte[] body)
        {
            if (body.Length == this.BodyLength)
            {
                if (body[body.Length-1] == 0x7B && body.Length == 18)
                {
                    DTU = Encoding.UTF8.GetString(body, 0, 11);
                    return true;
                }                
            }

            return false;
        }

        public bool OnParsingHeader(byte[] header)
        {
            //在该示例中，第一个字节表示后续的所有数据长度，但是header设置的是3，所以后续还应当接收length-2个长度。
            this.BodyLength = header[3] - 4;
            if (header[0] == 0x7B)
                return true;

            return false;
        }
    }

    public class DdpDataHandlingAdapter : CustomFixedHeaderDataHandlingAdapter<DdpRequestInfo>
    {
        public override int HeaderLength => 4;

        protected override DdpRequestInfo GetInstance()
        {
            return new DdpRequestInfo();
        }
    }
}
